package com.tensquare.qa.web.controller;

import java.awt.*;
import java.util.List;
import java.util.Map;

import com.tensquare.qa.client.LabelClient;
import dto.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import com.tensquare.qa.po.Problem;
import com.tensquare.qa.service.ProblemService;

import dto.PageResultDTO;
import dto.ResultDTO;

import javax.servlet.http.HttpServletRequest;

/**
 * 控制器层
 *
 * @author BoBoLaoShi
 */
@RestController
@CrossOrigin
@RequestMapping("/problem")
public class ProblemController {

    @Autowired
    private ProblemService problemService;

    @Autowired
    private HttpServletRequest request;

    /**
     * 增加
     * @param problem
     */
    @PostMapping
    public ResultDTO add(@RequestBody Problem problem) {
        //1.先鉴权，必须是普通用户角色才能发布问题
        Claims user_claims = (Claims) this.request.getAttribute("user_claims");
        if (user_claims == null){
            return new ResultDTO(false, StatusCode.ACCESSERROR, "权限不足");
        }
        // 补充发布问题的用户id
        problem.setNickname(user_claims.getSubject());
        problem.setId(user_claims.getId());
        problemService.saveProblem(problem);
        return new ResultDTO(true, StatusCode.OK, "增加成功");
    }

    /**
     * 修改
     *
     * @param problem
     */
    @PutMapping("/{id}")
    public ResultDTO edit(@RequestBody Problem problem, @PathVariable String id) {
        problem.setId(id);
        problemService.updateProblem(problem);
        return new ResultDTO(true, StatusCode.OK, "修改成功");
    }

    /**
     * 删除
     *
     * @param id
     */
    @DeleteMapping("/{id}")
    public ResultDTO remove(@PathVariable String id) {
        problemService.deleteProblemById(id);
        return new ResultDTO(true, StatusCode.OK, "删除成功");
    }

    /**
     * 查询全部数据
     *
     * @return
     */
    @GetMapping
    public ResultDTO list() {
        return new ResultDTO(true, StatusCode.OK, "查询成功", problemService.findProblemList());
    }

    /**
     * 根据ID查询
     *
     * @param id ID
     * @return
     */
    @GetMapping("/{id}")
    public ResultDTO listById(@PathVariable String id) {
        return new ResultDTO(true, StatusCode.OK, "查询成功", problemService.findProblemById(id));
    }

    /**
     * 根据条件查询
     *
     * @param searchMap
     * @return
     */
    @PostMapping("/search")
    public ResultDTO list(@RequestBody Map searchMap) {
        return new ResultDTO(true, StatusCode.OK, "查询成功", problemService.findProblemList(searchMap));
    }

    /**
     * 分页+多条件查询
     *
     * @param searchMap 查询条件封装
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @PostMapping("/search/{page}/{size}")
    public ResultDTO listPage(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<Problem> pageResponse = problemService.findProblemListPage(searchMap, page, size);
        return new ResultDTO(true, StatusCode.OK, "查询成功", new PageResultDTO<Problem>(pageResponse.getTotalElements(), pageResponse.getContent()));
    }


    /**
     * 根据base数据库label表id为1的数据
     * 按照最新的回复时间进行排序
     * 获取分页对象
     */
    @RequestMapping(value = "/newlist/{labelId}/{pageNum}/{pageSize}", method = RequestMethod.GET)
    public ResultDTO listPageNewRelayByLabelid(@PathVariable String labelId, @PathVariable int pageNum, @PathVariable int pageSize) {
        Page<Problem> page = this.problemService.findByLabelidOrOrderByReplytimeDesc(labelId, pageNum, pageSize);
        return new ResultDTO(true, StatusCode.OK, "查询成功", new PageResultDTO<Problem>(page.getTotalElements(), page.getContent()));
    }


    /**
     * 根据标签id[Java, Python, PHP...]查询热门回复的分页列表
     */
//    @RequestMapping(value = "/hotlist/{labelId}/{pageNum}/{size}", method = RequestMethod.GET)
    @RequestMapping(value = "/hotlist/{labelId}/{pageNum}/{pageSize}", method = RequestMethod.GET)
    public ResultDTO listPageHotRelayByLabelId(@PathVariable String labelId, @PathVariable int pageNum, @PathVariable int pageSize) {
        Page<Problem> page = this.problemService.findListByLabelIdOrderByReplyDesc(labelId, pageNum, pageSize);
        return new ResultDTO(true, StatusCode.OK, "查询成功", new PageResultDTO<Problem>(page.getTotalElements(), page.getContent()));
    }

    /**
     * 根据标签id[Java, Python, PHP...]查询未回复的分页列表
     */
    @RequestMapping(value = "/waitlist/{labelId}/{pageNum}/{pageSize}", method = RequestMethod.GET)
    public ResultDTO listPageWaitRelayByLabelId(@PathVariable String labelId, @PathVariable int pageNum, @PathVariable int pageSize) {
        Page<Problem> page = this.problemService.findListByLabelIdAndReplyNotOrderByCreatetimeDesc(labelId, pageNum, pageSize);
        return new ResultDTO(true, StatusCode.OK, "查询成功", new PageResultDTO<Problem>(page.getTotalElements(), page.getContent()));
    }

    @Autowired
    private LabelClient labelClient;

    @GetMapping("/label/{labelid}")
    public ResultDTO findLabelById(@PathVariable String labelid){
        ResultDTO resultDTO = this.labelClient.findById(labelid);
        return resultDTO;
    }
}
